/*
 * UniterruptableThreadDialog.java
 *
 * Created on March 12, 2009, 11:05 PM
 */

package net.sf.bluex.boundary;

import net.sf.bluex.components.monitors.ThreadProgressMonitor;
import net.sf.bluex.threads.MyThread;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  Blue
 */
public class ThreadsMonitor extends javax.swing.JDialog implements ListSelectionListener{

    private ThreadProgressMonitor tpm;

    private static ThreadsMonitor tm;
    
    /** Creates new form UniterruptableThreadDialog */
    private ThreadsMonitor(java.awt.Frame parent) {
        super(parent, false);
        initComponents();
        
        this.getRootPane().setDefaultButton(cmdCancel);

        lstThreads.addListSelectionListener(this);
        
        initListData();

        //start the thread progress monitor
        tpm=new ThreadProgressMonitor(lstThreads);
        
        this.setIconImage(net.sf.bluex.controller.UsefulMethods.getImage(net.sf.bluex.controller.FileModule.APP_ICON));
        this.setLocationRelativeTo(parent);
        this.setVisible(true);
    }
    
    private void initListData(){
        java.util.Vector<String> runningThreads=new java.util.Vector<String>();
        
        for(Thread t : MyThread.getRunningThreads()){
                runningThreads.add(t.getName());
        }
        
        lstThreads.setListData(runningThreads);
    }
    
    public static void showRunningThreads(java.awt.Frame parent){
        if(tm==null)
            tm=new ThreadsMonitor(parent);
        if(tm.isVisible())
            tm.requestFocus();
        else
            tm.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstThreads = new javax.swing.JList();
        cmdCancel = new javax.swing.JButton();
        cmdEndThread = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thread Monitor");

        jLabel1.setText("Following Threads are running:");

        jScrollPane1.setViewportView(lstThreads);

        cmdCancel.setText("Close");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        cmdEndThread.setText("Kill Thread");
        cmdEndThread.setEnabled(false);
        cmdEndThread.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEndThreadActionPerformed(evt);
            }
        });

        jLabel2.setText("This page is automatically refreshed in 2 seconds.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cmdEndThread)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                        .addComponent(cmdCancel))
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdEndThread))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmdEndThreadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEndThreadActionPerformed
// TODO add your handling code here:
        int row=lstThreads.getSelectedIndex();
        if(row!=-1){
            //with beep
            Toolkit.getDefaultToolkit().beep();
            int choice=JOptionPane.showConfirmDialog(this, "WARNING: Killing a running thread can cause undersired\n" +
                    "results including loss of data and object instability. The\n" +
                    "thread will not be given the chance to save its state or\n" +
                    "data before it is terminated. Are you sure you want to\n" +
                    "kill the thread?", "Thread Monitor Warning", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
            
            if(choice==JOptionPane.YES_OPTION){
                Vector<Thread> vect=MyThread.getRunningThreads();
                MyThread.removeThread((MyThread)vect.elementAt(row));
                vect.elementAt(row).stop();
                initListData();
            }
        }
}//GEN-LAST:event_cmdEndThreadActionPerformed

private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
// TODO add your handling code here:
    tpm.stop();
    this.dispose();
}//GEN-LAST:event_cmdCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdEndThread;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lstThreads;
    // End of variables declaration//GEN-END:variables

    public void valueChanged(ListSelectionEvent e) {
        cmdEndThread.setEnabled(!lstThreads.isSelectionEmpty());
    }

}
